package com.itcast.springboot.waste_recovery.controller;


import com.itcast.springboot.request.OrderManagesReq;
import com.itcast.springboot.response.OrderWasteShow;
import com.itcast.springboot.waste_recovery.common.CommonResult;
import com.itcast.springboot.waste_recovery.common.CommonResultCode;
import com.itcast.springboot.waste_recovery.service.OrderWasteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 所有订单废品信息 前端控制器
 * </p>
 *
 * @author 小破烂
 * @since 2020-08-24
 */
@RestController
@RequestMapping("/waste_recovery/order-waste")
@Api(tags = "订单废品信息类接口")
public class OrderWasteController {
    @Resource(name = "orderWasteServiceImpl")
    OrderWasteService orderWasteService;

    @GetMapping("/customer/waste_url/{order_id}")
    public CommonResult getWasteUrl(@PathVariable(value = "order_id") Integer orderId){
        List<String> wasteUrl = orderWasteService.getWasteUrl(orderId);
        return new CommonResult(CommonResultCode.CommonSuccess, wasteUrl);
    }

    @GetMapping("/staff/order_waste_show/{order_id}")
    @ApiOperation(value = "订单废品展示")
    @RequiresPermissions("staff-operated")       //拥有 staff-operated 才可以访问
    public CommonResult orderWastesShow(@PathVariable(value = "order_id") Integer orderId){
        List<OrderWasteShow> orderWasteShows = orderWasteService.orderWastesShow(orderId);
        return new CommonResult(CommonResultCode.CommonSuccess, orderWasteShows);
    }

    @PostMapping("/staff/total")
    @ApiOperation(value = "合计")
    @RequiresPermissions("staff-operated")       //拥有 staff-operated 才可以访问
    public CommonResult total(@RequestBody OrderManagesReq orderManages){
        Float total = orderWasteService.total(orderManages.getOrderManages());
        if (total < 0){
            return CommonResult.CommonFail();
        }
        return new CommonResult(CommonResultCode.CommonSuccess, total);
    }

    @PutMapping("/staff/orderManage/{order_id}")
    @ApiOperation(value = "订单处理")
    @RequiresPermissions("staff-operated")       //拥有 staff-operated 才可以访问
    public CommonResult orderManage(@PathVariable(value = "order_id") Integer orderId,
            @RequestBody OrderManagesReq orderManages){
        Integer result = orderWasteService.orderManage(orderId, orderManages.getOrderManages());
        if (result < 0){
            return CommonResult.CommonFail();
        }
        return CommonResult.CommonSuccess();
    }
}

